package com.wangzhixuan.commons.config;

import com.wangzhixuan.commons.Constant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * spring mvc 配置
 *
 * @author lixin
 * @since 2019年8月23日
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("OPTIONS", "POST", "GET", "DELETE")
                .allowedHeaders("Origin", "Content-Type", "Accept", "X-Token")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(Constant.VISIT_PATH + "/**").addResourceLocations("file:///" + Constant.UPLOAD_PATH + File.separator).setCachePeriod(31556926);
    }

    @Bean
    public net.dreamlu.module.ueditor.UeditorService ueditorService() {
        return new net.dreamlu.module.ueditor.UeditorService(new net.dreamlu.module.ueditor.UeditorManager());
    }
/*
    @Bean
    public MappingJackson2HttpMessageConverter objectMapper(ObjectMapper objectMapper){
        MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
                gen.writeString(StringUtils.EMPTY);
            }
        });
        converter.setObjectMapper(objectMapper);
        return converter;
    }*/
    
}
